package com.fss.article.controller;


import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyuncs.exceptions.ClientException;
import com.fss.article.domain.Article;
import com.fss.article.domain.ArticlePo;
import com.fss.article.domain.MyComment;
import com.fss.article.domain.Response;
import com.fss.article.service.ArticleService1;
import com.fss.common.core.controller.BaseController;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.net.URL;
import java.util.*;

import java.io.*;
import java.util.Random;


@RestController
@RequestMapping("/article")
public class ArticleController1 extends BaseController {

    @Autowired
    private ArticleService1 articleService1;

    @GetMapping("/getCategorys")
    public List<ArticlePo> getCategorys(){
        return articleService1.getCategorys();
    }

    @GetMapping("/getConArticle")
    public List<MyComment> getConArticle(@RequestParam String articleId){
        return articleService1.getMyComments(articleId);
    }

    @PostMapping("/addMyComment")
    public void addMyComment( @RequestParam String text,
                              @RequestParam String name,
                              @RequestParam int findId){
         articleService1.addMyComment(text,name,findId);
    }

    @PostMapping("/addArticle")
    public void addArticle(@RequestBody Article articleDTO) {
        System.out.println(articleDTO.toString());
        articleService1.addArticle(articleDTO);
    }

    @PostMapping("/upload")
    public Response uploadImage(@RequestParam("file") MultipartFile file) throws IOException, ClientException {
        String endpoint = "https://oss-cn-guangzhou.aliyuncs.com";
        String bucketName = generateUniqueBucketName("demo");
        // 替换为您的 Bucket 区域
        String region = "cn-guangzhou";
        // 创建 OSSClient 实例
        EnvironmentVariableCredentialsProvider credentialsProvider =
                CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(endpoint)
                .credentialsProvider(credentialsProvider)
                .region(region)
                .build();

            // 1. 创建存储空间（Bucket）
        ossClient.createBucket(bucketName);
        String name=UUID.randomUUID().toString()+".jpg";
        ossClient.putObject(bucketName, name, new ByteArrayInputStream(file.getBytes()));
        GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, name, HttpMethod.GET);
        Date expiration = new Date(new Date().getTime() + 2*30*24*3600 * 1000L);
        // 通过HTTP GET请求生成签名URL。
        request.setExpiration(expiration);
        URL signedUrl = ossClient.generatePresignedUrl(request);
        // 打印签名URL。
        System.out.println("signed url for getObject: " + signedUrl);
        ossClient.shutdown();


        return new Response(true,signedUrl.toString() );
    }

    public static String generateUniqueBucketName(String prefix) {
        // 获取当前时间戳
        String timestamp = String.valueOf(System.currentTimeMillis());
        // 生成一个 0 到 9999 之间的随机数
        Random random = new Random();
        int randomNum = random.nextInt(10000); // 生成一个 0 到 9999 之间的随机数
        // 连接以形成一个唯一的 Bucket 名称
        return prefix + "-" + timestamp + "-" + randomNum;
    }
}
